﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;

namespace OpenPhotoAlbum.BusinessLogics.Email
{
    public static class EmailSender
    {
        public static void Send(
            IEmailContentBuilder contentBuilder,
            IEmailMessageBuilder messageBuilder,
            IEmailSmtpClientBuilder smtpClientBuilder)
        {
            MailMessage mailMessage = messageBuilder.BuildMessage();
            
            mailMessage.Subject = contentBuilder.BuildSubject();
            mailMessage.Body = contentBuilder.BuildBody();

            SmtpClient smtpClient = smtpClientBuilder.BuildSmtpClient();

            try { smtpClient.Send(mailMessage); }
            catch (Exception exception) { throw new FailedToSendEmailException(exception); }
        }
    }

    public class FailedToSendEmailException : Exception
    {
        private const string MESSAGE = "Failed to send email.";

        public FailedToSendEmailException(Exception innerException)
            : base(MESSAGE, innerException)
        {
        }
    }
}
